package work43;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * @Author admin
 * @Date 2019/4/3 8:15
 */
public class SongDaoImp implements SongDao{
    ArrayList<Song> arrayList=new ArrayList<Song>();

    @Override
    public void init() {
    Song song1=new Song("你好");
    Song song2=new Song("不好");
    Song song3=new Song("再见");
    arrayList.add(song1);
    arrayList.add(song2);
    arrayList.add(song3);
    System.out.println("初始化歌曲列表");
    Iterator iterable=arrayList.iterator();
        System.out.println();
    while (iterable.hasNext()){
        System.out.println(iterable.next());
    }
        System.out.println();
    }

    @Override
    public void add(Song name) {

    }

    @Override
    public void all() {
        System.out.println("初始化歌曲列表");
        Iterator iterable=arrayList.iterator();
        while (iterable.hasNext()){
            Song song=(Song) iterable.next();
            System.out.println(song.getName()+" ");
        }
        System.out.println();
    }

    @Override
    public void top(String name) {
    Song song=select(name);
    if (song==null){
        System.out.println("当前歌曲列表没有此歌曲");
    }else {
        arrayList.remove(song);
        arrayList.add(0,song);
        System.out.println("已将歌曲"+song.getName()+"置顶");
    }
    }

    @Override
    public Song select(String name) {
        for (int i = 0; i <arrayList.size() ; i++) {
            if (arrayList.get(i).getName().equals(name)){
                return arrayList.get(i);
            }
        }
        return null;
    }

    @Override
    public void before(Song music) {
        int index=arrayList.indexOf(music);
        arrayList.remove(music);
        arrayList.add(index-1,music);
    }
}
